package xyz.csmcool.imchat.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import xyz.csmcool.imchat.entity.FileMessageVo;
import xyz.csmcool.imchat.entity.PrivateChats;
import xyz.csmcool.imchat.entity.vo.RoomChatsVo;
import xyz.csmcool.imchat.service.IPrivateChatsService;
import xyz.csmcool.imutil.dto.LastMessageDto;
import xyz.csmcool.imutil.dto.LastPrivateMessageDto;
import xyz.csmcool.imutil.dto.Message;
import xyz.csmcool.imutil.dto.PrivateMessage;
import xyz.csmcool.imutil.utils.JwtUtils;
import xyz.csmcool.imutil.utils.ResultTool;
import xyz.csmcool.imutil.webVo.MyPage;
import xyz.csmcool.imutil.webVo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csmcool
 * @since 2022-08-24
 */
@RestController
@RequestMapping("/privateChats")
@RequiredArgsConstructor
public class PrivateChatsController {

    final IPrivateChatsService privateChatsService;

    @PostMapping("sendMessage")
    public ResultVo sendMessage(@RequestBody PrivateMessage<String> message, HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        privateChatsService.sendPrivateMessage(message,uid);
        return ResultTool.success();
    }

    @PostMapping("sendSoundMessage")
    public ResultVo sendSoundMessage(HttpServletRequest request,@RequestParam("recodeSound") MultipartFile file,
                                     @RequestParam("receiveId") Integer receiveId) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        privateChatsService.sendPrivateSoundMessage(file,uid,receiveId);
        return ResultTool.success();
    }

    @PostMapping("sendImageMessage")
    public ResultVo sendImageMessage(HttpServletRequest request,@RequestParam("fileImage") MultipartFile[] files,
                                     @RequestParam("receiveId") Integer receiveId) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        privateChatsService.sendPrivateImageMessage(files,uid,receiveId);
        return ResultTool.success();
    }

    @PostMapping("/sendRoomFileMessage")
    public ResultVo sendRoomFileMessage(HttpServletRequest request,@RequestBody FileMessageVo messageVo) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        privateChatsService.sendPrivateFileMessage(messageVo,id);
        return ResultTool.success();
    }

    @GetMapping("/getHistoryPrivateMessage/{limit}/{page}/{fid}/{id}")
    public ResultVo<MyPage<PrivateChats>> getHistoryPrivateMessage(@PathVariable("limit") Long limit,
                                                           @PathVariable("page") Long page,
                                                           @PathVariable("fid") Integer roomId,
                                                           @PathVariable("id") Integer id,
                                                           HttpServletRequest request) {
        String token = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<PrivateChats> historyPrivateMessage = privateChatsService.getHistoryPrivateMessage(page, limit, roomId, id,
                Integer.valueOf(token));
        return ResultTool.success(historyPrivateMessage);
    }

    @GetMapping("/getLastPrivateMessage")
    public ResultVo<List<LastPrivateMessageDto>> getLastPrivateMessage(@RequestParam("fids") List<Integer> ids,
                                                                @RequestParam("uid") Integer uid) {
        List<LastPrivateMessageDto> lastMessages = privateChatsService.getLastMessage(ids,uid);
        return ResultTool.success(lastMessages);
    }
}
